package com.lse_from.control

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.lse_from.dto.FromData
import com.lse_from.dto.FromField
import com.lse_from.dto.Froms
import com.lse_from.exception.LseException
import com.lse_from.service.FromDataService
import com.lse_from.service.FromFieldService
import com.lse_from.service.FromsService
import com.lse_from.utils.PubUtil
import com.lse_from.utils.PubVal
import com.lse_from.utils.ResponseResult
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*
import javax.annotation.Resource
import javax.servlet.http.HttpServletRequest
import javax.validation.constraints.NotBlank

/**
 * 表单列表控制
 *
 * @author 祁雪
 */
@Controller
@RequestMapping(value = arrayOf("/lse/api/from"))
@Validated
class FromController : LseException(), PubVal {

    @Resource
    lateinit var request: HttpServletRequest

    @Autowired
    lateinit var fromsService: FromsService

    @Autowired
    lateinit var fromDataService: FromDataService

    @Autowired
    lateinit var fromFieldService: FromFieldService

    /**
     * 编辑or新建表单
     */
    @PostMapping(value = arrayOf("/ecfrom"))
    @ResponseBody
    fun ecFrom(@Validated froms: Froms): ResponseResult {

        if (froms.id.isNullOrBlank()) {
            froms.id = PubUtil.getUUID()
        }

        if (froms.tableName.isNullOrBlank()) {
            froms.tableName = "new_table"
        }

        val flag = fromsService.saveOrUpdate(froms)

        return ResponseResult.custom(flag, if (flag) "保存成功" else "保存失败")

    }

    /**
     * 删除表单配置
     * PS: 表单内有数据则不允许删除
     */
    @PostMapping(value = arrayOf("/delform"))
    @ResponseBody
    fun delForm(@NotBlank(message = "请选择表单") form: String): ResponseResult {

        // 检查是否有数据
        val dataSize = fromDataService.count(QueryWrapper<FromData>().let {
            it.eq("from_id", form)
            it
        })

        if (dataSize > 0) {
            return ResponseResult.err("该表单内含有数据,无法删除", null)
        }

        fromFieldService.remove(QueryWrapper<FromField>().let {
            it.eq("from_id", form)
            it
        })
        fromDataService.remove(QueryWrapper<FromData>().let {
            it.eq("from_id", form)
            it
        })

        val flag = fromsService.removeById(form)

        return ResponseResult.custom(flag, if (flag) "删除成功" else "删除失败")
    }

    /**
     * 读取列表
     */
    @GetMapping(value = arrayOf("/list"))
    @ResponseBody
    fun list(
        @RequestParam(defaultValue = PubVal.PAGENO) pageNo: Int,
        @RequestParam(defaultValue = PubVal.PAGESIZE) pageSize: Int
    ): ResponseResult {

        val page = fromsService.page(Page<Froms>(pageNo.toLong(), pageSize.toLong()))

        return ResponseResult.sucData(page)

    }

}